<?php

/**
 * Created by PhpStorm.
 * User: CFH
 * Date: 2017/3/30
 * Time: 19:07
 */
class mark_mdl extends Model
{
    public static $table = 'mark_hot';


    public function save_mark($tb_uid, $nick, $mark){
        $rs = $this->get_mark($tb_uid);
        if(empty($rs['openid'])){
            $tags = $this->dbh->insert(self::$table, array(
                'openid' => $tb_uid,
                'nick' => $nick,
                'mark' => $mark,
                'last_time' => date('Y-m-d H:i:s')
            ));
            $rs = $this->dbh->insert_id();
        }else{
            $this->dbh->update(self::$table,
                array(
                    'nick' => $nick,
                    'mark' => $mark,
                    'last_time' => date('Y-m-d H:i:s')
                ),
                'openid="'.$rs['openid'].'"'
            );
        }
        $rs = $this->get_mark($tb_uid);

        return $rs;
    }


    public function get_mark($tb_uid){
        $r = '';
        if(!empty($tb_uid)){
            $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE to_days(last_time) = to_days(now())
					AND openid="'.$tb_uid.'"'
            ;

            $r = $this->dbh->get_one($sql);
        }
        return $r;
    }

    public function get_mark_list_day(){
        $r = '';

        $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE to_days(last_time) = to_days(now())
                    ORDER BY mark DESC
				    LIMIT 0,10'
        ;

        $r = $this->dbh->get_all($sql);

        return $r;
    }

}